package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.monitor.entity.ErpInsurance;
import com.monitor.service.ErpInsuranceService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆保险 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-12
 */
@RestController
@RequestMapping("/erpInsurance")
@Api(description="车辆保险")
public class ErpInsuranceController {


    @Resource
    private ErpInsuranceService insuranceService;

    @RequestMapping("/inertOrUpdateInsurance")
    @ApiOperation("添加Or修改车辆保险表")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateInsurance(ErpInsurance insurance,Integer custId) {
        Result<Integer> result=new Result<Integer>();
        try{
            insurance.setCustomerId(custId);
            result.setCount(1);
            result.setData(insuranceService.insertOrUpdate(insurance)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getInsuranceById")
    @ApiOperation("根据车辆保险id查看车辆保险信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
           .
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "insuranceId", value = "车辆保险ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpInsurance> getInsuranceById(Integer insuranceId) {
        Result<ErpInsurance> result=new Result<ErpInsurance>();
        try{
            result.setCount(1);
            result.setData(insuranceService.selectById(insuranceId));
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getInsuranceBycustIdPage")
    @ApiOperation("根据客户ID查询车辆保险分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Page<ErpInsurance>> getUserBycustIdPage(@RequestParam(value = "page", defaultValue = "1") int page,
                                              @RequestParam(value="limit",defaultValue="10") int limit, Integer custId) {
        Result<Page<ErpInsurance>> result=new Result<Page<ErpInsurance>>();
        try{
            Page<ErpInsurance> page1=new Page<ErpInsurance>(page,limit);

            Page<ErpInsurance> carPage=insuranceService.selectPage(page1,new EntityWrapper<ErpInsurance>().eq("customer_id",custId));
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setCount(carPage.getRecords().size());
            result.setData(carPage);
        }catch (Exception e){e.printStackTrace();}
        return result;
    } @RequestMapping("/getInsuranceByCustId")
    @ApiOperation("根据客户ID查询车辆保险分页")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpInsurance>> getInsuranceByCustId(Integer custId) {
        Result<List<ErpInsurance>> result=new Result<List<ErpInsurance>>();
        try{

            List<ErpInsurance> insuranceByCustId = insuranceService.getInsuranceByCustId(custId);
            //Page<ErpCustomerCar> page=new Page<ErpCustomerCar>(1,2);
            result.setData(insuranceByCustId);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removeInsurance")
    @ApiOperation("删除车辆保险信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "insuranceId", value = "车辆保险ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removeInsurance(Integer insuranceId) {
        Result<Integer>  result=new Result<Integer>();
        try{

            result.setCount(1);
            result.setData(insuranceService.deleteById(insuranceId)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


}

